package controllers

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
)

type SiteRep struct {
	Data    interface{}
	Results interface{}
}

func TencentMapSite(stages, city string) (float64, float64) {
	value := url.Values{}
	// value.Set("keyword", fmt.Sprintf("%s[地铁站]", stages))
	// value.Set("boundary", fmt.Sprintf("region(%s,0)", city))
	// value.Set("page_size", "1")
	// value.Set("key", "D2QBZ-DGX6D-4BG4C-HXU2W-754JK-WQBBN")
	value.Set("keyword", stages)
	value.Set("boundary", fmt.Sprintf("region(%s,0)", city))
	// value.Set("filter", fmt.Sprintf("=category=%s", "地铁站"))
	value.Set("category", "地铁站")
	value.Set("page_size", "1")
	value.Set("key", "D2QBZ-DGX6D-4BG4C-HXU2W-754JK-WQBBN")

	resp, err := http.Get(fmt.Sprintf("http://apis.map.qq.com/ws/place/v1/search?%s", value.Encode()))
	if err != nil {
		log.Fatalf("Get TencentMapSite init error:% s", err.Error())
	}
	defer resp.Body.Close()
	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Println("TencentMapSite body error:", err)
		return 0, 0
	}

	// log.Println("data", string(data))
	var obj SiteRep
	if err := json.Unmarshal(data, &obj); err != nil {
		log.Println("TencentMapSite to struct error:", err, string(data))
		// log.Println("json to struct error:", err)
		return 0, 0
	}
	list, ok := obj.Data.([]interface{})
	if !ok || len(list) == 0 {
		// log.Println("TencentMapSite list", list)
		log.Println("TencentMapSite to []interface{} error", stages, city)
		return 0, 0
	}

	index, ok := list[0].(map[string]interface{})
	if !ok {
		log.Println("index", index)
		log.Println("TencentMapSite to map[string]interface{} error", stages, city)
		return 0, 0
	}
	info, ok := index["location"]
	if !ok {
		log.Println("TencentMapSite to location error")
		return 0, 0
	}

	result := info.(map[string]interface{})
	// log.Println("data", , result["lng"].(float64))
	// var lat, lng float64

	return result["lat"].(float64), result["lng"].(float64)
}

func TencentMapSite2(stages, city string) (float64, float64) {
	value := url.Values{}
	value.Set("keyword", stages)
	value.Set("boundary", fmt.Sprintf("region(%s,0)", city))
	// value.Set("filter", fmt.Sprintf("=category=%s", "地铁站"))
	value.Set("category", "地铁站")
	value.Set("page_size", "1")
	value.Set("key", "D2QBZ-DGX6D-4BG4C-HXU2W-754JK-WQBBN")

	resp, err := http.Get(fmt.Sprintf("http://apis.map.qq.com/ws/place/v1/search?%s", value.Encode()))
	if err != nil {
		log.Fatalf("Get TencentMapSite init error:% s", err.Error())
	}
	defer resp.Body.Close()
	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Println("TencentMapSite body error:", err)
		return 0, 0
	}

	log.Println("data", string(data))
	var obj SiteRep
	if err := json.Unmarshal(data, &obj); err != nil {
		log.Println("TencentMapSite to struct error:", err, string(data))
		// log.Println("json to struct error:", err)
		return 0, 0
	}
	list, ok := obj.Data.([]interface{})
	if !ok || len(list) == 0 {
		// log.Println("TencentMapSite list", list)
		log.Println("TencentMapSite to []interface{} error", stages, city)
		return 0, 0
	}

	index, ok := list[0].(map[string]interface{})
	if !ok {
		log.Println("index", index)
		log.Println("TencentMapSite to map[string]interface{} error", stages, city)
		return 0, 0
	}
	info, ok := index["location"]
	if !ok {
		log.Println("TencentMapSite to location error")
		return 0, 0
	}

	result := info.(map[string]interface{})
	// log.Println("data", , result["lng"].(float64))
	// var lat, lng float64

	return result["lat"].(float64), result["lng"].(float64)
}

func BaiduMapSite(stages, city string) (float64, float64) {
	value := url.Values{}
	value.Set("query", stages)
	value.Set("region", city)
	value.Set("output", "json")
	value.Set("tag", "地铁站")
	value.Set("city_limit", "true")
	value.Set("tag", "地铁站")
	value.Set("scope", "1")
	// value.Set("ak", "a5yQBqnf9hnh5d8UTsV7fKxZxACBjPya")
	value.Set("ak", "ymakzhtWqnwwbbdG3z7VftKVPxrXrFG4")
	// value.Set("page_size", "1")
	resp, err := http.Get(fmt.Sprintf("http://api.map.baidu.com/place/v2/search?%s", value.Encode()))
	if err != nil {
		log.Fatalf("Get BaiduMapSite init error:% s", err.Error())
	}
	defer resp.Body.Close()
	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Println("BaiduMapSite body error:", err)
		return 0, 0
	}

	// log.Println("data", string(data))
	var obj SiteRep
	if err := json.Unmarshal(data, &obj); err != nil {
		log.Println("BaiduMapSite to struct error:", err, string(data))
		// log.Println("json to struct error:", err)
		return 0, 0
	}
	// log.Println("obj", obj.Results)
	list, ok := obj.Results.([]interface{})
	if !ok || len(list) == 0 {
		log.Println("BaiduMapSite to []interface{} error", stages, city)
		return 0, 0
	}

	index, ok := list[0].(map[string]interface{})
	if !ok {
		log.Println("index", index)
		log.Println("BaiduMapSite to map[string]interface{} error", stages, city, string(data))
		return 0, 0
	}
	info, ok := index["location"]
	if !ok {
		log.Println("BaiduMapSite to location error", string(data))
		return 0, 0
	}

	result := info.(map[string]interface{})
	// log.Println("data", , result["lng"].(float64))
	// var lat, lng float64

	return result["lat"].(float64), result["lng"].(float64)
}
func BaiduMapSite2(stages, city string) (float64, float64) {
	value := url.Values{}
	value.Set("query", stages)
	value.Set("region", city)
	value.Set("output", "json")
	// value.Set("tag", "地铁站")
	value.Set("tag", "有轨列车站")

	value.Set("city_limit", "true")
	value.Set("scope", "1")
	// value.Set("ak", "a5yQBqnf9hnh5d8UTsV7fKxZxACBjPya")
	value.Set("ak", "ymakzhtWqnwwbbdG3z7VftKVPxrXrFG4")

	// value.Set("page_size", "1")
	resp, err := http.Get(fmt.Sprintf("http://api.map.baidu.com/place/v2/search?%s", value.Encode()))
	if err != nil {
		log.Fatalf("Get BaiduMapSite init error:% s", err.Error())
	}
	defer resp.Body.Close()
	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Println("BaiduMapSite body error:", err)
		return 0, 0
	}

	log.Println("data", value.Encode(), string(data))
	var obj SiteRep
	if err := json.Unmarshal(data, &obj); err != nil {
		log.Println("BaiduMapSite to struct error:", err, string(data))
		// log.Println("json to struct error:", err)
		return 0, 0
	}
	// log.Println("obj", obj.Results)
	list, ok := obj.Results.([]interface{})
	if !ok || len(list) == 0 {
		log.Println("BaiduMapSite to []interface{} error", stages, city)
		return 0, 0
	}

	index, ok := list[0].(map[string]interface{})
	if !ok {
		log.Println("index", index)
		log.Println("BaiduMapSite to map[string]interface{} error", stages, city, string(data))
		return 0, 0
	}
	info, ok := index["location"]
	if !ok {
		log.Println("BaiduMapSite to location error", string(data))
		return 0, 0
	}

	result := info.(map[string]interface{})
	// log.Println("data", , result["lng"].(float64))
	// var lat, lng float64

	return result["lat"].(float64), result["lng"].(float64)
}

// (22.325723,114.198554)
